package com.population.content;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.logindesign.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class PopulationDetailActivity extends Activity {
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String ENDPOINT = "http://192.168.1.198:1536/CommunitySystemsicWsWF.asmx";

	// 按钮3个
	private ImageButton btnBack;

	// textview控件
	private TextView textViewName;
	private TextView textViewSex;
	private TextView textViewHzxm;
	private TextView textViewIdcd;
	private TextView textViewHzgx;
	private TextView textViewOldNm;
	private TextView textViewMinzu;
	private TextView textViewCsrq;
	private TextView textViewCsdz;
	private TextView textViewJgdz;	
	private TextView textViewJtzz;
	private TextView textViewSflz;
	private TextView textViewTechang;
	private TextView textViewZzbh;
	private TextView textViewZfqk;
	private TextView textViewShengao;
	private TextView textViewBlood;
	private TextView textViewZzmm;
	private TextView textViewWhcd;
	private TextView textViewByqk;
	private TextView textViewJkzk;
	private TextView textViewHyzk;
	private TextView textViewJob;
	private TextView textViewSzdw;
	private TextView textViewSjhm;

	// TextView的变量值
	private String id;
	private String textViewNameValue = "";
	private String textViewSexValue = "";
	private String textViewHzxmValue = "";
	private String textViewIdcdValue = "";
	private String textViewHzgxValue = "";
	private String textViewOldNmValue = "";
	private String textViewMinzuValue = "";
	private String textViewCsrqValue = "";
	private String textViewCsdzValue = "";
	private String textViewJgdzValue = "";	
	private String textViewJtzzValue = "";
	private String textViewSflzValue = "";
	private String textViewTechangValue = "";
	private String textViewZzbhValue = "";
	private String textViewZfqkValue = "";
	private String textViewShengaoValue = "";
	private String textViewBloodValue = "";
	private String textViewZzmmValue = "";
	private String textViewWhcdValue = "";
	private String textViewByqkValue = "";
	private String textViewJkzkValue = "";
	private String textViewHyzkValue = "";
	private String textViewJobValue = "";
	private String textViewSzdwValue = "";
	private String textViewSjhmValue = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_population_detail);
		// sharedpreference中取得相应值
		getValueFromSharedPreferences();
		// 初始化TextView
		initTextView();
	}

	/**
	 * sharedpreference中取得相应值
	 */
	private void getValueFromSharedPreferences() {
		// TODO Auto-generated method stub
		SharedPreferences sharedPreferences = getSharedPreferences("UserInfo",
				Activity.MODE_PRIVATE);
		id = sharedPreferences.getString("userid", "");
	}
	
	/**
	 * 初始化TextView及button
	 */
	private void initTextView() {
		// TODO Auto-generated method stub
		// findviewbyid
		textViewName = (TextView) findViewById(R.id.textView_population_Name);
		textViewSex = (TextView) findViewById(R.id.textView_population_Sex);
		textViewHzxm = (TextView) findViewById(R.id.textView_population_Hzxm);
		textViewIdcd = (TextView) findViewById(R.id.textView_population_Idcd);
		textViewHzgx = (TextView) findViewById(R.id.textView_population_Hzgx);
		textViewOldNm = (TextView) findViewById(R.id.textView_population_OldNm);
		textViewMinzu = (TextView) findViewById(R.id.textView_population_Minzu);
		textViewCsrq = (TextView) findViewById(R.id.textView_population_Csrq);
		textViewCsdz = (TextView) findViewById(R.id.textView_population_Csdz);
		textViewJgdz = (TextView) findViewById(R.id.textView_population_Jgdz);
		textViewJtzz = (TextView) findViewById(R.id.textView_population_Jtzz);
		textViewSflz = (TextView) findViewById(R.id.textView_population_Sflz);
		textViewTechang = (TextView) findViewById(R.id.textView_population_Techang);
		textViewZzbh = (TextView) findViewById(R.id.textView_population_Zzbh);
		textViewZfqk = (TextView) findViewById(R.id.textView_population_Zfqk);
		textViewShengao = (TextView) findViewById(R.id.textView_population_Shengao);
		textViewBlood = (TextView) findViewById(R.id.textView_population_Blood);
		textViewZzmm = (TextView) findViewById(R.id.textView_population_Zzmm);
		textViewWhcd = (TextView) findViewById(R.id.textView_population_Whcd);
		textViewByqk = (TextView) findViewById(R.id.textView_population_Byqk);
		textViewJkzk = (TextView) findViewById(R.id.textView_population_Jkzk);
		textViewHyzk = (TextView) findViewById(R.id.textView_population_Hyzk);
		textViewJob = (TextView) findViewById(R.id.textView_population_Job);
		textViewSzdw = (TextView) findViewById(R.id.textView_population_Szdw);
		textViewSjhm = (TextView) findViewById(R.id.textView_population_Sjhm);
		
		
		
		// 四个按钮
		btnBack = (ImageButton) findViewById(R.id.btn_population_list_back);

		// 返回按钮
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 跳转到listview页面
				Intent intent = new Intent();
				intent.setClass(PopulationDetailActivity.this,
						PopulationListViewActivity.class);
				startActivity(intent);
			}
		});

		// setText 耗时的操作放到线程里去
		new setTextViewValueTask().execute();
	}

	/**
	 * 设置textView的值
	 */
	private class setTextViewValueTask extends AsyncTask<Void, Void, String> {

		protected void onPreExecute() {
			// 设置TextView的值
			setTextViewValue();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			// 事件详细参数需要 id,unitcode
			Intent getdetail = getIntent();
			id = getdetail.getStringExtra("id");
			String resultString = getData();
			Log.e("webservice取得", "完毕");
			String finallyResultString = jsonFormat(resultString);
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 设置TextView的值
				setTextViewValue();
			}
		}

	}

	/**
	 * 得到详细信息
	 * 
	 * @return
	 */
	private String getData() {
		// TODO Auto-generated method stub
		String resultString = "";
		String methodName = "get_PopulationQueryDetails";
		String soapAcion = "http://tempuri.org/get_PopulationQueryDetails";
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
		// 事件管理id
		soapObject.addProperty("id", id);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAcion, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
				Log.e("webservice取得", resultString);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultString;
	}

	/**
	 * 解析json数据返回hashmap格式数据
	 * 
	 * @return
	 */
	private String jsonFormat(String resultString) {
		// TODO 由于事件管理详细webservice返回值多写了对中括号，导致数据崩溃，所以先用的固定值
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				// 根据实际从第2组开始取得有关数据
				jsonObject = jsonArray.optJSONObject(1);
				
				textViewNameValue = jsonObject.optString("name");
				textViewSexValue = jsonObject.optString("gender");
				textViewHzxmValue = jsonObject.optString("holdname");
				textViewIdcdValue = jsonObject.optString("idnumber");
				textViewHzgxValue = jsonObject.optString("hzgx");
				textViewOldNmValue = jsonObject.optString("oldname");
				textViewMinzuValue = jsonObject.optString("minzu");
			    textViewCsrqValue = jsonObject.optString("birthdate");
				textViewCsdzValue = jsonObject.optString("birthplace");
				textViewJgdzValue = jsonObject.optString("holdaddress");
				textViewJtzzValue = jsonObject.optString("jtdz");
				textViewSflzValue = jsonObject.optString("louzhang");
				textViewTechangValue = jsonObject.optString("techang");
				textViewZzbhValue = jsonObject.optString("zzbh");
				textViewZfqkValue = jsonObject.optString("housing");
				textViewShengaoValue = jsonObject.optString("standing");
				textViewBloodValue = jsonObject.optString("bloodtype");
				textViewZzmmValue = jsonObject.optString("zzmm");
				textViewWhcdValue = jsonObject.optString("education");
				textViewByqkValue = jsonObject.optString("bingyi");
				textViewJkzkValue = jsonObject.optString("health");
				textViewHyzkValue = jsonObject.optString("hyzk");
				textViewJobValue = jsonObject.optString("zy");
				textViewSzdwValue = jsonObject.optString("dw");
				textViewSjhmValue = jsonObject.optString("phone");

				return "success";
			} else {
				// 没有数据，如何显示？已经到最后了
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}

	/**
	 * 设置TextView的值
	 */
	private void setTextViewValue() {
		// TODO Auto-generated method stub
		textViewName.setText(textViewNameValue);
		textViewSex.setText(textViewSexValue);
		textViewHzxm.setText(textViewHzxmValue);
		textViewIdcd.setText(textViewIdcdValue);
		textViewHzgx.setText(textViewHzgxValue);
		textViewOldNm.setText(textViewOldNmValue);
		textViewMinzu.setText(textViewMinzuValue);
		textViewCsrq.setText(textViewCsrqValue);
		textViewCsdz.setText(textViewCsdzValue);
		textViewJgdz.setText(textViewJgdzValue);	
		textViewJtzz.setText(textViewJtzzValue);
		textViewSflz.setText(textViewSflzValue);
		textViewTechang.setText(textViewTechangValue);
		textViewZzbh.setText(textViewZzbhValue);
		textViewZfqk.setText(textViewZfqkValue);
		textViewShengao.setText(textViewShengaoValue);
		textViewBlood.setText(textViewBloodValue);
		textViewZzmm.setText(textViewZzmmValue);
		textViewWhcd.setText(textViewWhcdValue);
		textViewByqk.setText(textViewByqkValue);
		textViewJkzk.setText(textViewJkzkValue);
		textViewHyzk.setText(textViewHyzkValue);
		textViewJob.setText(textViewJobValue);
		textViewSzdw.setText(textViewSzdwValue);
		textViewSjhm.setText(textViewSjhmValue);
	}

	/**
	 * 跳转到listview页面
	 */
	private void skipListViewActivity() {
		Intent listviewIntent = new Intent();
		listviewIntent.setClass(PopulationDetailActivity.this,
				PopulationListViewActivity.class);
		PopulationDetailActivity.this.startActivity(listviewIntent);
		PopulationDetailActivity.this.finish();
	}

	/**
	 * 重写keydown事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			skipListViewActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
